package me.lc.hammerheads.sprite;

import me.lc.hammerheads.R;
import me.lc.hammerheads.core.GameManager;
import me.lc.hammerheads.core.GameSystem;
import me.lc.hammerheads.core.StageManager;
import me.lc.hammerheads.entity.GameStageInfo;
import me.lc.hammerheads.scene.MainScene;
import me.lc.hammerheads.util.ADHelper;
import me.lc.hammerheads.util.AudioHelper;
import me.lc.hammerheads.util.GameUtil;
import android.app.Activity;

import com.wiyun.engine.nodes.Button;
import com.wiyun.engine.nodes.Label;
import com.wiyun.engine.nodes.Node;
import com.wiyun.engine.nodes.Sprite;
import com.wiyun.engine.types.WYColor3B;
import com.wiyun.game.WiGame;

/**
 * 胜利画面Block
 * 
 * @author Administrator
 * 
 */
public class WinShowBlock extends Node {

	Sprite bg;
	Sprite star1;
	Sprite star2;
	Sprite star3;
	Button btnNext;
	Button btnRetry;
	Button btnMainmenu;
	Label gameScore;
	Label comboScore;
	Label restTime;
	Label totalScore;
	Label addGold;

	static int normalFontSize = 20;
	static int addGoldFontSize = 15;

	public WinShowBlock() {
		bg = Sprite.make(GameSystem.winBgTex);
		addChild(bg);

		float labelX = GameSystem.winBgTex.getWidth() * 0.589f
				- GameSystem.winBgTex.getWidth() / 2.0f;
		float labelY1 = GameSystem.winBgTex.getHeight() * 0.75f
				- GameSystem.winBgTex.getHeight() / 2.0f;
		gameScore = Label.make(0 + "", normalFontSize);
		gameScore.setAnchorPercent(0, 0);
		gameScore.setColor(WYColor3B.make(0, 0, 0));
		gameScore.setPosition(labelX, labelY1);
		addChild(gameScore);

		float labelY2 = GameSystem.winBgTex.getHeight() * 0.63f
				- GameSystem.winBgTex.getHeight() / 2.0f;
		comboScore = Label.make(0 + "", normalFontSize);
		comboScore.setAnchorPercent(0, 0);
		comboScore.setColor(WYColor3B.make(0, 0, 0));
		comboScore.setPosition(labelX, labelY2);
		addChild(comboScore);

		float labelY3 = GameSystem.winBgTex.getHeight() * 0.51f
				- GameSystem.winBgTex.getHeight() / 2.0f;
		restTime = Label.make(0 + "", normalFontSize);
		restTime.setAnchorPercent(0, 0);
		restTime.setColor(WYColor3B.make(0, 0, 0));
		restTime.setPosition(labelX, labelY3);
		addChild(restTime);

		float labelY4 = GameSystem.winBgTex.getHeight() * 0.39f
				- GameSystem.winBgTex.getHeight() / 2.0f;
		totalScore = Label.make(0 + "", normalFontSize);
		totalScore.setColor(WYColor3B.make(180, 0, 0));
		totalScore.setAnchorPercent(0, 0);
		totalScore.setPosition(labelX, labelY4);
		addChild(totalScore);

		float labelY5 = GameSystem.winBgTex.getHeight() * 0.294f
				- GameSystem.winBgTex.getHeight() / 2.0f;
		addGold = Label.make(0 + "", addGoldFontSize);
		addGold.setColor(WYColor3B.make(180, 130, 0));
		addGold.setAnchorPercent(0, 0);
		addGold.setPosition(labelX, labelY5);
		addChild(addGold);

		// 设置按钮位置
		btnNext = Button.make(R.drawable.button_next, R.drawable.button_next,
				this, "nextBtnClicked");
		float nextY = GameSystem.winBgTex.getHeight() * 0.215f
				- GameSystem.winBgTex.getHeight() / 2.0f;
		btnNext.setPosition(0, nextY);
		btnNext.setClickScale(1.05f);
		addChild(btnNext);

		btnRetry = Button.make(R.drawable.button_retry_win,
				R.drawable.button_retry_win, this, "retryBtnClicked");
		btnRetry.setAnchorPercent(0, 0);
		float retryX = GameSystem.winBgTex.getWidth() * 0.081f
				- GameSystem.winBgTex.getWidth() / 2.0f;
		float retryY = GameSystem.winBgTex.getHeight() * 0.085f
				- GameSystem.winBgTex.getHeight() / 2.0f;
		btnRetry.setPosition(retryX, retryY);
		btnRetry.setClickScale(1.05f);
		addChild(btnRetry);

		btnMainmenu = Button.make(R.drawable.button_returnmenu_win,
				R.drawable.button_returnmenu_win, this, "menuBtnClicked");
		btnMainmenu.setAnchorPercent(0, 0);
		float menuX = GameSystem.winBgTex.getWidth() * 0.547f
				- GameSystem.winBgTex.getWidth() / 2.0f;
		btnMainmenu.setPosition(menuX, retryY);
		btnMainmenu.setClickScale(1.05f);
		addChild(btnMainmenu);

		// star 暂时不用star
		// float starY = GameSystem.winBgTex.getHeight() * 0.889f
		// - GameSystem.winBgTex.getHeight() / 2.0f;
		// star1 = Sprite.make(GameSystem.star1Tex);
		// star1.setPosition(0, starY);
		// star1.setVisible(false);
		// addChild(star1);
		//
		// star2 = Sprite.make(GameSystem.star2Tex);
		// star2.setPosition(0, starY);
		// star2.setVisible(false);
		// addChild(star2);
		//
		// star3 = Sprite.make(GameSystem.star3Tex);
		// star3.setPosition(0, starY);
		// star3.setVisible(false);
		// addChild(star3);
	}

	// TODO 林晨 需要改成50关卡
	private static int stageCounts = 30;

	public void showScore() {
		btnNext.setVisible(true);
		// star1.setVisible(false);
		// star2.setVisible(false);
		// star3.setVisible(false);

		int total = setScoreSprites();

		// 统计消灭矮人总数
		if (GameManager.instance.knockOutCount > 0) {
			StageManager.instance.allDwarfCount += GameManager.instance.knockOutCount;
			StageManager.instance.saveAllDwarfCount();
		}

		AudioHelper.win();
		if (GameManager.instance.gameType == 1) {
			showStageModeWin(total);
		} else {
			showUnlimitModeWin(total);
		}
	}

	private void showUnlimitModeWin(int total) {
		btnNext.setVisible(false);
		// 争分夺秒分数
		WiGame.submitScore("2c7dfad14f867521", total, null, true);
	}

	private void showStageModeWin(int total) {
		if (StageManager.instance.currentStage + 1 < stageCounts) {
			btnNext.setVisible(true);

			GameStageInfo nextInfo = StageManager.instance.stageInfoLst
					.get(StageManager.instance.currentStage + 1);

			nextInfo.setLocked(false);

			if (!StageManager.instance.stageScore.containsKey(nextInfo
					.getStageName())) {
				StageManager.instance.stageScore
						.put(nextInfo.getStageName(), 0);
			}

		} else {
			btnNext.setVisible(false);
			// 完成所有关卡
			WiGame.unlockAchievement("a42bb7a440e0b7ad");
		}

		// 保存本轮分数
		GameStageInfo currentStageInfo = StageManager.instance.getCurrentInfo();
		if (total > currentStageInfo.getScore()) {
			StageManager.instance.stageScore.put(
					currentStageInfo.getStageName(), total);
			currentStageInfo.setScore(total);
		}

		// 保存数据
		Thread saveThread = new Thread() {
			@Override
			public void run() {
				StageManager.instance.saveStageScore();
				StageManager.instance.saveGoldCount();
			}
		};
		saveThread.start();

		// 提交总分数微云
		int sum = 0;
		for (GameStageInfo stageInfo : StageManager.instance.stageInfoLst) {
			if (!stageInfo.isLocked() && stageInfo.getScore() > 0) {
				sum += stageInfo.getScore();
			}
		}

		System.out.println("微云总分:" + sum);

		// 提交分数
		// 关卡总分
		WiGame.submitScore("edeed7135bbf9b25", sum, null, true);

		if (StageManager.instance.currentStage == 0) {
			// 完成牛刀小试
			WiGame.unlockAchievement("6a8c3b38c0202ce0");
		} else if (StageManager.instance.currentStage == 4) {
			// 完成崭露头角
			WiGame.unlockAchievement("3f5795f10cd9713b");
		} else if (StageManager.instance.currentStage == 9) {
			// 矮人恐慌
			WiGame.unlockAchievement("525500b0c8b6066f");
		}

	}

	private int setScoreSprites() {
		int dwarfScore = GameManager.instance.getDwarfScore();
		if (dwarfScore < 0) {
			dwarfScore = 0;
		}
		float rateTemp = GameManager.instance.getSuccessRate();
		int accuracyRate = 0;
		if (rateTemp == 1) {
			accuracyRate = 2000;
		} else {
			accuracyRate = (int) (1000 * rateTemp);
		}
		int health = 0;
		if (!GameManager.instance.isAddedHeart()) {
			health = GameManager.instance.getLeftHealth() * 200;
		}
		int total = dwarfScore + accuracyRate + health;
		int gold = total / 77;
		gameScore.setText(dwarfScore + "");
		comboScore.setText(accuracyRate + "");
		restTime.setText(health + "");
		totalScore.setText(total + "");
		addGold.setText(gold + "");

		StageManager.instance.goldCount += gold;
		return total;
	}

	/**
	 * 显示关卡星星
	 * 
	 * @param total
	 */
	private void showStar(int total) {
		// Sprite star = star1;
		// if (total >= GameSystem.Star3Score) {
		// star = star3;
		// } else if (total >= GameSystem.Star2Score) {
		// star = star2;
		// }
		// star.setVisible(true);
		// IntervalAction bigger = (IntervalAction) ScaleTo.make(0.5f, 0.8f,
		// 1.1f)
		// .autoRelease();
		// IntervalAction smaller = (IntervalAction) bigger.reverse()
		// .autoRelease();
		// IntervalAction seq = (IntervalAction) Sequence.make(
		// EaseBackInOut.make(bigger), EaseBackInOut.make(smaller))
		// .autoRelease();
		// Action repeat = (Action) RepeatForever.make(seq).autoRelease();
		// star.runAction(repeat);
	}

	/**
	 * 消除总数成就
	 */
	private void totalClearCountHonor() {
		// int total = StageManager.instance.totalCouples;
		// if (total >= 20000) {
		// WiGame.unlockAchievement("2e5e22138ff5746f");
		// } else if (total >= 10000) {
		// WiGame.unlockAchievement("74f72a5f3c6a826e");
		// } else if (total >= 5000) {
		// WiGame.unlockAchievement("f01a79a33da2f42c");
		// } else if (total >= 2000) {
		// WiGame.unlockAchievement("aaba2f99b7564592");
		// } else if (total >= 500) {
		// WiGame.unlockAchievement("b0d9849323ccd589");
		// } else if (total >= 100) {
		// WiGame.unlockAchievement("3120b1702260b9df");
		// }
	}

	public void nextBtnClicked() {
		((Activity) GameSystem.AndroidContext).runOnUiThread(new Runnable() {
			public void run() {
				AudioHelper.buttonClick();
				// 消除总数
				WiGame.submitScore("4ea8369eec1c3cdd",
						StageManager.instance.allDwarfCount, null, true);
				// totalClearCountHonor();
				StageManager.instance.currentStage++;
				GameManager.instance
						.startStageGame(StageManager.instance.currentStage);
				WinShowBlock.this.setScale(0.5f);
				if (StageManager.instance.enableAudio == 1) {
					AudioHelper.playMusicInGame();
				}
			}
		});
	}

	public void retryBtnClicked() {
		((Activity) GameSystem.AndroidContext).runOnUiThread(new Runnable() {
			public void run() {
				AudioHelper.buttonClick();
				// 消除总数
				WiGame.submitScore("4ea8369eec1c3cdd",
						StageManager.instance.allDwarfCount, null, true);
				// totalClearCountHonor();
				if (GameManager.instance.gameType == 1) {
					GameManager.instance
							.startStageGame(StageManager.instance.currentStage);
				} else {
					GameManager.instance.startUnlimitGame();
				}
				WinShowBlock.this.setScale(0.5f);
				if (StageManager.instance.enableAudio == 1) {
					AudioHelper.playMusicInGame();
				}
			}
		});
	}

	public void menuBtnClicked() {
		((Activity) GameSystem.AndroidContext).runOnUiThread(new Runnable() {
			public void run() {
				AudioHelper.buttonClick();
				// 消除总数
				WiGame.submitScore("4ea8369eec1c3cdd",
						StageManager.instance.allDwarfCount, null, true);
				// totalClearCountHonor();
				MainScene.make().showMainMenu();
				GameManager.instance.quitGame();
				GameUtil.switchSceneFlipX(MainScene.make());
				ADHelper.hideAd();
				WinShowBlock.this.setScale(0.5f);
				if (StageManager.instance.enableAudio == 1) {
					AudioHelper.playMusicMenu();
				}
			}
		});
	}
}
